import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:gold/base/provider_scaffold.dart';
import 'package:gold/ui/my/order/order_config.dart';
import 'package:gold/ui/my/order/refund_viewmodel.dart';
import 'package:gold/utils/xfg_color.dart';
import 'package:gold/utils/xfg_size.dart';
import 'package:gold/widgets/common_widgets.dart';
import 'package:gold/widgets/http_image.dart';
import 'package:gold/widgets/xfg_text.dart';
import 'package:provider/provider.dart';

/// @description: 退款页面
/// @author tangzhentao
/// @created at 22:49 2025/2/27

class RefundPage extends StatelessWidget {
  /// 订单id
  final int orderId;
  /// 金额
  final double price;
  /// 积分
  final int points;
  /// 商品logo
  final String? productLogo;
  /// 商品名称
  final String? productName;

  const RefundPage({super.key, required this.orderId, required this.price, required this.points, this.productLogo, this.productName});

  @override
  Widget build(BuildContext context) {
    String moneyDesc = '';
    if (price > 0) {
      moneyDesc = "¥$price";
    }
    if (points > 0) {
      moneyDesc = '$moneyDesc${moneyDesc.isNotEmpty ? ' + ':''}$points积分';
    }
    return ProviderScaffold<RefundViewmodel>(
      model: RefundViewmodel(orderId),
      onInit: (data) => data.load(),
      appBar: (_) => xfgAppbar(title: '退款中'),
      builder: (ctx, refund) {
        final detail = refund.detail;
        final refundNo = detail?.id?.toString()?? '';
        String progress = '';
        if (detail?.logs?.isNotEmpty == true) {
          final log = detail!.logs!.first;
          progress = '${log.content?? ''}  ${log.createdAt}';
        }
        return SingleChildScrollView(
        padding: EdgeInsets.fromLTRB(XfgSize().size10, XfgSize().size24, XfgSize().size10, XfgSize().bottomBarHeight == 0 ? XfgSize().size10:XfgSize().bottomBarHeight),
        child: Column(
          children: [
            Container(
              margin: EdgeInsets.only(left: XfgSize().size8),
              alignment: Alignment.centerLeft,
              child: XfgText('已申请${detail?.type == 0 ? '仅退款':'退款退货'}，待卖家处理', fontSize: XfgSize().text20, color: XfgColor.text1D, bold: FontWeight.bold),
            ),
            Builder(builder: (con) {
              final minutes = con.select<RefundViewmodel, int>((k) => k.minutes);
              final Widget child;
              if (minutes == 0) {
                child = XfgText('过期卖家未处理，将会自动退款，若卖家拒绝可申请维权', fontSize: XfgSize().text14, color: XfgColor.text2, height: 1.5);
              } else {
                child = XfgText(_getMinutesDesc(minutes), fontSize: XfgSize().text14, color: XfgColor.theme, height: 1.5, children: [
                  TextSpan(text: '后卖家未处理，将会自动退款，若卖家拒绝可申请维权', style: TextStyle(color: XfgColor.text2))
                ]);
              }
              return Container(
                  margin: EdgeInsets.only(left: XfgSize().size8, top: XfgSize().size12),
                  alignment: Alignment.centerLeft,
                  child: child
              );
            }),
            Container(
              margin: EdgeInsets.only(top: XfgSize().size15),
              decoration: white6Decoration,
              padding: EdgeInsets.fromLTRB(XfgSize().size10, XfgSize().size10, XfgSize().size10, XfgSize().size14),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  XfgText('退款进度', fontSize: XfgSize().text16, color: XfgColor.text1D, bold: FontWeight.bold),
                  SizedBox(height: XfgSize().size10),
                  Row(
                    children: [
                      Container(
                          margin: EdgeInsets.only(right: XfgSize().size5),
                          width: XfgSize().size5, height: XfgSize().size5, 
                          decoration: BoxDecoration(shape: BoxShape.circle, color: XfgColor.refundApplyingCirce)
                      ),
                      Expanded(child: XfgText(progress.isEmpty ? '暂无':progress, fontSize: XfgSize().text14, color: XfgColor.refundApplyingCirce))
                    ],
                  ),
                  SizedBox(height: XfgSize().size10),
                  Row(
                    children: [
                      SizedBox(width: XfgSize().size10),
                      Expanded(child: XfgText(getRefundReasonDesc(detail?.reason), color: XfgColor.text3))
                    ],
                  ),
                ],
              ),
            ),
            /// 退款详情
            GestureDetector(
              onTap: () => refund.toDetailPage(moneyDesc),
              child: Container(
                margin: EdgeInsets.only(top: XfgSize().size10),
                decoration: white6Decoration,
                child: Column(
                  children: [
                    Container(
                      margin: EdgeInsets.fromLTRB(XfgSize().size10, XfgSize().size18, 0, XfgSize().size20),
                      alignment: Alignment.centerLeft,
                      child: XfgText('退款详情', fontSize: XfgSize().text16, color: XfgColor.text1D, bold: FontWeight.bold),
                    ),
                    /// 商品详情
                    Container(
                      margin: EdgeInsets.only(bottom: XfgSize().size6),
                      padding: EdgeInsets.symmetric(horizontal: XfgSize().size12),
                      child: Row(
                        children: [
                          HttpImage(productLogo, size: Size.square(XfgSize().size40), radius: BorderRadius.circular(XfgSize().size2)),
                          SizedBox(width: XfgSize().size10),
                          Expanded(
                              child: Column(
                                mainAxisAlignment: MainAxisAlignment.center,
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  XfgText(productName?? '', fontSize: XfgSize().text14, color: XfgColor.text1D),
                                  SizedBox(height: XfgSize().size8),
                                  XfgText(moneyDesc, fontSize: XfgSize().text14, color: XfgColor.text1D),
                                ],
                              )
                          )
                        ],
                      ),
                    ),
                    _itemWidget('退款类型', detail?.type == 0 ? '仅退款':'退款退货'),
                    _itemWidget('退款原因', getRefundReasonDesc(detail?.reason)),
                    _itemWidget('退款金额', moneyDesc, valueColor: XfgColor.text1D),
                    _itemWidget('申请时间', detail?.createdAt?? ''),
                    Padding(
                      padding: EdgeInsets.fromLTRB(XfgSize().size12, XfgSize().size14, XfgSize().size10, XfgSize().size14),
                      child: Row(
                        children: [
                          XfgText('退款编号', fontSize: XfgSize().text14, color: XfgColor.text1D),
                          Expanded(child: XfgText(refundNo, fontSize: XfgSize().text14, color: XfgColor.text3, textAlign: TextAlign.end)),
                          Visibility(
                            visible: refundNo.isNotEmpty,
                            child: Container(
                              margin: EdgeInsets.only(left: XfgSize().size10),
                              child: Builder(builder: (con) => GestureDetector(
                                onTap: () {
                                  Clipboard.setData(ClipboardData(text: refundNo)).then((_) {
                                    xfgShowSnackBar(con, '退款编号', refundNo);
                                  });
                                },
                                child: XfgText('复制', fontSize: XfgSize().text14, color: XfgColor.text1D, bold: FontWeight.bold),
                              )),
                            ),
                          )
                        ],
                      ),
                    )
                  ],
                ),
              ),
            )
          ],
        ),
        );
      }
    );
  }

  /// 获取分钟描述
  String _getMinutesDesc(int minutes) {
    final day = (minutes / (60 * 24)).toInt();
    final remain = (minutes % (60 * 24)).toInt();
    final hour = (remain / 60). toInt();
    final minute = (remain % 60).toInt();
    return '$day天$hour小时$minute分';
  }

  /// 退款信息通用Widget
  Widget _itemWidget(String text, String value, {Color? valueColor}) => Padding(
    padding: EdgeInsets.fromLTRB(XfgSize().size12, XfgSize().size14, XfgSize().size10, XfgSize().size14),
    child: Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        XfgText(text, fontSize: XfgSize().text14, color: XfgColor.text1D),
        XfgText(value, fontSize: XfgSize().text14, color: valueColor?? XfgColor.text3),
      ],
    ),
  );
}
